<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="/houtai/js/vue.js"></script>
    <script type="text/javascript" src="/houtai/js/jquery-2.1.4.js"></script>

    <link rel="stylesheet" href="/houtai/layuiadmin/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <!--设置layUI表格样式，自动换行-->
    <style>
        .layui-table-cell{
            height:35px;
            line-height: 35px;
            /*overflow:visible;*/
            /*text-overflow:inherit;*/
            /*white-space:normal;*/
        }
    </style>
</head>
<body>

<!--------------------------------------头部工具条------------------------------------------>
<script type="text/html" id="headbar">
    <div class="layui-btn-container"  >
        <!--使用表达式让按钮动态显示，if条件为true则显示，false不显示-->
        <!--{{#  if(insert){ }}-->
        <!--<button type="button" class="layui-btn" lay-event="insert">增加-->
        <!--<i class="layui-icon">&#xe654;</i>-->
        <!--</button>-->
        <!--{{#  } }}-->

        <!--判断是否有删除权限-->
        {{#  if(del){ }}
        <button class="layui-btn layui-btn-danger" lay-event="getCheckData">批量删除
            <i class="layui-icon">&#xe640;</i>
        </button>
        {{#  } }}
    </div>
</script>

<!-------------------------------------------- 每一行工具条 --------------------------------------->
<script type="text/html" id="barDemo">
    <!--判断是否有修改权限-->
    {{#  if(update){ }}
    <button type="button" class="layui-btn layui-btn-xs layui-btn-radius layui-btn-warm" lay-event="singleUpdate">
        <i class="layui-icon">&#xe642;</i> 回复
    </button>
    {{#  } }}

    <!--判断是否有删除权限-->
    {{#  if(del){ }}
    <button type="button" class="layui-btn layui-btn-xs layui-btn-radius layui-btn-danger" lay-event="singleDel">
        <i class="layui-icon">&#xe640;</i> 删除
    </button>
    {{#  } }}
</script>

<!---------------------------------------------条件搜索框------------------------------------------->
<div class="demoTable" style="margin-top: 10px">
    <form id="serachForm" class="layui-form">
        <div class="layui-inline" style="margin-top: 15px">
            <div class="layui-form-item" style="width: 200px;float: left;margin-right: 10px">
                <label class="layui-form-label">订单状态</label>
                <div class="layui-input-block">
                    <select name="isanswer" id="isanswer" lay-filter="aihao">
                        <option value="0" selected>请选择</option>
                        <option value="1" >已回复</option>
                        <option value="2">待回复</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-inline" style="margin-top: 15px">
            <div class="layui-form-item" style="width: 200px">
                <label class="layui-form-label">评论分值</label>
                <div class="layui-input-block">
                    <select name="startPoint" id="startPoint" lay-filter="aihao">
                        <option value="0" selected></option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                    </select>
                </div>
            </div>
        </div>
        -
        <div class="layui-inline" style="width: 100px">
            <select name="endPoint" id="endPoint" lay-filter="aihao">
                <option value="0" selected></option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
        </div>


        <div class="layui-inline">
            <label class="layui-form-label">评论日期</label>
            <div class="layui-input-inline" style="width: 100px">
                <input type="text" name="startDate" id="startDate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
            -
            <div class="layui-input-inline" style="width: 100px">
                <input type="text" name="endDate" id="endDate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline" style="margin-left: 10px">
            <!--这个地方的不能直接使用button标签，使用button标签会重复提交数据-->
            <input type="button" class="layui-btn" data-type="reload" value="搜索">
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>

    </form>
</div>

<!-----------------------------------------表格内容--------------------------------------------->
<div id="content">
    <table class="layui-table"
           lay-data="{url:'/houtai/page/products/comment/list',page:true,limit:10,limits:[10,20,10000],toolbar:'#headbar',
                     height:'full-100',loading:true,title:'用户信息表',
                     //把返回的数据result转为换layUI需要的格式，此处返回的是mybatisPlus的IPage对象，具体返回值自己设定
                     parseData: function(res){ //res 即为原始返回的数据
                        return {
                            'code': res.code, //解析接口状态
                            'msg': res.message, //解析提示文本
                            'count': res.obj.totalSize, //解析数据长度
                            'data': res.li //解析数据列表
                          };
                       },
                //给表格设置一个ID
               id:'test'}"
           lay-filter="filter">
        <thead>
        <tr>
            <!--开起全选框-->
            <th lay-data="{checkbox:true,fixed:'left'}"></th>
            <th lay-data="{field:'commentId',hide:true}">评论ID</th>
            <!--返回对象中另一个对象的属性-->
            <th lay-data="{field:'user',
                templet:function (d) {
                    return d.user.username;
                }
            }">下单人</th>
            <th lay-data="{field:'product',
                templet:function (d) {
                    return d.product.productName;
                }
            }">商品名称</th>
            <th lay-data="{field:'commentDesc',width:200}">评论详情</th>

            <th lay-data="{field:'commentPoint',sort: true}">评论星数</th>
            <th lay-data="{field:'commentImg',templet:'#imgTpl'}">评论图片</th>
            <th lay-data="{field:'commentTime',sort: true}">评论时间</th>
            <th lay-data="{field:'commentAnswer'}">回复内容</th>
            <!--判断是否回复-->
            <th lay-data="{field:'isanswer',minWidth:110,sort: true,
                templet:function (d) {
                    if(d.isanswer == 1){
                        return '是';
                    }else if(d.isanswer == 2){
                        return '否';
                    }
                }
            }">是否回复</th>
            <!--最后一列操作列，有修改或删除的权限就显示这列，否者不显示，通过Vue实现-->
            <th v-if="isShow" lay-data="{field:'opeartion', width:170,fixed: 'right', toolbar: '#barDemo'}">操作</th>
        </tr>
        </thead>
    </table>
</div>

<!-----------这里是修改弹出层的表单信息,表单的id用于表单的选择，style是在本页隐藏，只有点击编辑才会弹出---------->
<div class="layui-row" id="popUpdateUser" style="display:none;">
    <form class="layui-form" id="updateForm" lay-filter="updateFormFilter">
        <input name="commentId" hidden>
        <div class="layui-form-item layui-form-text" style="margin-top: 20px">
            <label class="layui-form-label">回复内容</label>
            <div class="layui-input-block" style="width: 300px">
                <textarea name="commentAnswer" placeholder="请输入回复内容" class="layui-textarea" lay-verify="notNull"></textarea>
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: 50px">
            <div class="layui-input-block">
                <input type="button" class="layui-btn" lay-submit lay-filter="updateSubmit" value="立即提交">
            </div>
        </div>
    </form>
</div>

<script src="/houtai/layuiadmin/layui/layui.js" charset="utf-8"></script>
<script src="/houtai/layuiadmin/layui/lay/modules/util.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<!--动态显示图片-->
<script type="text/html" id="imgTpl">
    <ul>
    {{# for(var i = 0;i < d.commentImg.length;i++){  }}
        <img style="width: 30px;height: 30px" onclick="showBigImage(this)" src="{{d.commentImg[i].commentImgSrc}}" >
    {{# } }}
    </ul>
</script>
<script>

    //定义四个全局变量用于是否显示增加、删除、修改按钮的权限验证
    var insert = false;//是否具备新增权限，默认不具备
    var del= false;
    var update= false;
    var isShow=false;
    //利用Vue判断是否具备每一行的删除或者修改权限
    var vue = new Vue({
        el:"#content",
        data:{
            buttons:[]
        },
        computed:{
            isShow:function () {
                var _this = this;
                var delbtn=false;
                var updatebtn=false;
                //获取当前网页地址
                var url = location.href.split("localhost:8080")[1];
                $.ajax({
                    url: "/houtai/adminuser/buttons",
                    type: "get",
                    async: false,
                    data: {
                        url: url
                    },
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        _this.buttons = data.li;
                        for(var i=0;i<_this.buttons.length;i++){
                            if(_this.buttons[i].menuUrl.indexOf("insert")!=-1){
                                insert = true;
                                break;
                            }
                        }
                        for(var i=0;i<_this.buttons.length;i++){
                            if(_this.buttons[i].menuUrl.indexOf("delete")!=-1){
                                del = true;
                                isShow = true;
                                delbtn = true;
                                break;
                            }
                        }
                        for(var i=0;i<_this.buttons.length;i++){
                            if(_this.buttons[i].menuUrl.indexOf("update")!=-1){
                                update = true;
                                isShow = true;
                                updatebtn =  true;
                                break;
                            }
                        }

                    }
                });

                if(delbtn||updatebtn){
                    return true;
                }else{
                    return false;
                }

            }
        }
    });

    //点击图片放大
    function showBigImage(e) {
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            shadeClose: true, //点击阴影关闭
            area: [$(e).width + 'px', $(e).height + 'px'], //宽高
            content: "<img src=" + $(e).attr('src') + " />"
        });
    };


    layui.use(['table','jquery','laypage','util','laydate','form','layer'], function(){
        var table = layui.table;
        var $ = layui.jquery;
        var util = layui.util;
        var laydate = layui.laydate;
        var form = layui.form;
        var layer = layui.layer;

        //验证表单内容
        form.verify({
            notNull:function (value, item) {
                if(value == null || value.trim() == ''){
                    return "回复内容不能为空";
                }
            }
        });
        //加载日期选择插件
        laydate.render({
            elem: '#startDate'
        });
        laydate.render({
            elem: '#endDate'
        });
        //刷新表单
        form.render();
        //监听顶部工具栏事件
        table.on('toolbar(filter)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            if(data.length < 1){
                layer.msg("至少选择一行哦!!!");
            }else{
                switch(obj.event){
                    //点击批量删除执行以下代码
                    case 'getCheckData':
                        layer.confirm('真的删除行么', function(index){
                            layer.close(index);
                            var data = checkStatus.data;
                            var commentIds = new Array();
                            for(var i = 0;i < data.length;i++){
                                commentIds.push(data[i].myorderId);
                            }
                            console.log(commentIds);
                            var s = JSON.stringify(commentIds);
                            console.log(s);
                            $.ajax({
                                url:"/houtai/page/products/comment/delete",
                                data:{commentIds:s},
                                type:"post",
                                dataType:"json",
                                success:function (data) {
                                    if(data.message == "success"){
                                        layer.msg("删除成功",{icon: 6});
                                        //重载表格
                                        $(".layui-laypage-btn").click();
                                    }else if(data.message == "premissionException"){
                                        location.href = "/houtai/page/exception/NoPremission.html";
                                    }else if(data.message == "serverException"){
                                        location.href = "/houtai/page/exception/500.html";
                                    }else{
                                        layer.msg("删除失败", {icon: 5});
                                    }
                                }
                            })
                        });
                        break;
                };
            }

        });

        //监听每一行工具条
        table.on('tool(filter)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            console.log(data);
            if(layEvent === 'detail'){//查看
                //do somehing
            } else if(layEvent === 'singleDel'){ //删除
                layer.confirm('真的删除行么', function(index){
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        url:"/houtai/page/products/comment/delete",
                        data:{commentIds: data.commentId},
                        type:"post",
                        dataType:"json",
                        success:function (data) {
                            if(data.message == "success"){
                                layer.msg("删除成功",{icon: 6});
                                //重载表格
                                $(".layui-laypage-btn").click();
                            }else if(data.message == "premissionException"){
                                location.href = "/houtai/page/exception/NoPremission.html";
                            }else if(data.message == "serverException"){
                                location.href = "/houtai/page/exception/500.html";
                            }else{
                                layer.msg("删除失败", {icon: 5});
                            }
                        }
                    })
                });
            } else if(layEvent === 'singleUpdate'){ //编辑
                if(data.isanswer == 1){
                    layer.msg("这条评论已经回复过了哦");
                }else{
                    form.val('updateFormFilter', {
                        "commentId": data.commentId
                    });

                    //开起弹出层
                    layer.open({
                        //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        type: 1,
                        title: "回复评论",
                        area: ['450px', '400px'],//设置弹出层尺寸
                        content: $("#popUpdateUser")//引用的弹出层的页面层的方式加载修改界面表单
                    });
                }
            }
        });

        //监听表单提交，上述的submit()中的值为事件过滤器的值
        form.on('submit(updateSubmit)', function(data){
            console.log(data.field.commentAnswer) //当前容器的全部表单字段，名值对形式：{name: value}
            if(data.field.commentAnswer != null || data.field.commentAnswer.trim() != ''){
                $.ajax({
                    url:"/houtai/page/products/comment/update",
                    data:{commentId:data.field.commentId,commentAnswer:data.field.commentAnswer},
                    type:"post",
                    dataType:"json",
                    success:function (data) {
                        if(data.message == "success"){
                            //关闭弹出层
                            layer.closeAll();
                            layer.msg("回复成功");
                            //重载表格
                            $(".layui-laypage-btn").click();
                        }else if(data.message == "premissionException"){
                            location.href = "/houtai/page/exception/NoPremission.html";
                        }else if(data.message == "serverException"){
                            location.href = "/houtai/page/exception/500.html";
                        }else{
                            layer.msg("删除失败", {icon: 5});
                        }
                    }
                })
            }
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        //设置搜索框查询条件
        var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#demoReload');
                var str = $("#serachForm").serialize();
                //执行重载
                table.reload('test', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        isanswer:$("#isanswer").val(),
                        startPoint:$("#startPoint").val(),
                        endPoint:$("#endPoint").val(),
                        startDate:$("#startDate").val(),
                        endDate:$("#endDate").val()
                    }
                }, 'data');
            }
        };

        //监听搜索框提交按钮
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听修改框提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });
</script>

</body>
</html>